package com.baijiaxing.weixin.family.service.impl;

import com.baijiaxing.weixin.family.client.service.FamilyMenuService;
import com.baijiaxing.weixin.family.common.consts.FamilyTokenUtil;
import com.baijiaxing.weixin.family.common.http.HttpRequest;
import com.baijiaxing.weixin.family.common.http.HttpRequestModel;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.http.NameValuePair;
import org.apache.http.entity.StringEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Copyright  BeijingDMCC Co. Ltd.
 * All right reserved
 * 创建日期 16/12/18
 *
 * @Title
 * @Description：FamilyMenuServiceImpl
 * @author：yinhongke
 * @email：hongke.yin@datamining.cn
 * @mender:
 * @version：1.0
 * @since：
 * @Remark：
 */
@Service
public class FamilyMenuServiceImpl implements FamilyMenuService {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private FamilyTokenUtil familyTokenUtil;


    public void createMenu() {
        try {
            String accessToken = familyTokenUtil.getToken();
            JSONObject oneJson = new JSONObject();
            JSONArray twoArray = new JSONArray();

            JSONObject twoJson = new JSONObject();
            twoJson.put("type", "click");
            twoJson.put("name", "小学入学");
            twoJson.put("key", "1");
            twoArray.add(twoJson);

            twoJson = new JSONObject();
            twoJson.put("type", "click");
            twoJson.put("name", "接访系统");
            twoJson.put("key", "2");
            twoArray.add(twoJson);

            twoJson = new JSONObject();
            twoJson.put("type", "click");
            twoJson.put("name", "统计分析");
            twoJson.put("key", "3");
            twoArray.add(twoJson);

            oneJson.put("button", twoArray);


            logger.info("menustrjson:{}", oneJson.toString());

            String url = "https://qyapi.weixin.qq.com/cgi-bin/menu/create?access_token=" + accessToken + "&agentid=0";


            List<NameValuePair> parameters = new ArrayList<NameValuePair>();
            StringEntity stringEntity = new StringEntity(oneJson.toString(), "UTF-8");
            stringEntity.setContentEncoding("application/json");
            HttpRequestModel d = HttpRequest.httpRequestPost(url, parameters, stringEntity);
            System.out.println(d.getContent());
            System.out.println(d.getStatusCode());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
